package yxy.game.pm2.command.chibi;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BattleMessage;
import yxy.game.pm2.message.ChibiMessage;
import yxy.game.pm2.module.chibi.ChibiHandle;

/** 火烧赤壁 火烧赤壁挑战开始 */
@MessageType(proto = Command.ChibiChanllengeExecute, classtype = ChibiMessage.ChibiChanllengeExecuteRequest.class)
public class CMD_ChibiChanllengeExecute extends Command<ChibiMessage.ChibiChanllengeExecuteRequest> {

	@Override
	public void execute(MessageSource messageSource, ChibiMessage.ChibiChanllengeExecuteRequest request) {
		String playerCode = messageSource.getTag();
		BattleMessage.BattleExecuteRequest battleReq = request.getBattleReq();

		ChibiMessage.ChibiChanllengeExecuteResponse.Builder response = ChibiMessage.ChibiChanllengeExecuteResponse.newBuilder();

		ChibiHandle handle = new ChibiHandle();
		if (handle.isBattling(playerCode)) {
			send(messageSource, response, Result.BattleIsRunning);// 战斗尚未结束
			return;
		}

		try {
			response.setBattleResp(handle.battleResponseExecute(playerCode, battleReq));
			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, ChibiMessage.ChibiChanllengeExecuteResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}